#include "Acceptor.h"

CAcceptor::CAcceptor( boost::asio::io_service& io_service, unsigned short port )
	: m_Acceptor( io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port) )
{
}

CAcceptor::~CAcceptor( void )
{
}

void CAcceptor::Accept( void )
{
	// Start an accept operation for a new connection.
	ConnectionPtr new_conn( new CConnection(m_Acceptor.get_io_service()) );
	m_Acceptor.async_accept( new_conn->GetSocket(), boost::bind(&CAcceptor::OnAccept, this, boost::asio::placeholders::error, new_conn) );
}

void CAcceptor::OnAccept( const boost::system::error_code& e, ConnectionPtr conn )
{
	if( e )
	{
		OnAcceptFailed( e, conn );
		return;
	}

	OnAcceptSuccessed( conn );
}

void CAcceptor::OnRead( const boost::system::error_code& e, size_t bytes_transferred, ConnectionPtr conn )
{
	if( e )
	{
		OnReadFailed( e, bytes_transferred, conn );
		return;
	}

	OnReadSuccessed( bytes_transferred, conn );
}

void CAcceptor::OnWrite( const boost::system::error_code& e, ConnectionPtr conn )
{
	if( e )
	{
		OnWriteFailed( e, conn );
		return;
	}

	OnWriteSuccessed( conn );
}